package com.example.su_shi_tan_bas_lib.album.ui

import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.example.su_shi_tan_bas_lib.R
import com.example.su_shi_tan_bas_lib.adapter.JssBaseViewHolder
import com.example.su_shi_tan_bas_lib.album.bean.AlbumFile
import com.example.su_shi_tan_bas_lib.base.SimpleListFragment
import com.example.su_shi_tan_bas_lib.bean.CommonBean
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

class AlbumPreviewFooterListFragment : SimpleListFragment<AlbumFile>() {

    companion object {
        fun newInstance() = AlbumPreviewFooterListFragment()
    }

    private val mCheckItem = mutableListOf<AlbumFile>()

    private var mListItemClick: PreviewFooterListItemClick? = null

    fun setListItemClick(mListItemClick: PreviewFooterListItemClick) {
        this.mListItemClick = mListItemClick
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport=false
        isInitialRefresh = false
        isEnableLoadMore = false
        isAlwaysRefresh = false
    }

    fun addData(newData: List<AlbumFile>) {
        mCheckItem.addAll(newData)
        mData.addAll(mCheckItem)
    }

    fun setCurrentIndex(index: Int) {
        currentIndex = if (index >= 0 && index < mCheckItem.size) index else 0

    }

    fun setCurrentIndexCheck(index: Int, check: Boolean) {
        if (currentIndex != index) {
            currentIndex = if (index >= 0 && index < mCheckItem.size) index else 0
            if (mRecyclerView != null) {
                mRecyclerView.scrollToPosition(currentIndex)
            }
        }
        if (mAdapter != null && currentIndex < mAdapter.data.size) {
            mAdapter.getItem(currentIndex).isChecked = check
            mAdapter.notifyDataSetChanged()
        }
    }

    private var currentIndex = 0

    override fun getItemLayout() = R.layout.fragment_album_preview_footer_list_item

    override fun initView(view: View) {
        super.initView(view)
        mSwipeRefreshLayout.isEnabled = false
        mRecyclerView.scrollToPosition(currentIndex)

    }

    override fun addItemDecoration(): RecyclerView.ItemDecoration {
        val decoration =
            DividerItemDecoration(_mActivity, LinearLayoutManager.HORIZONTAL)
        decoration.setDrawable(
            ContextCompat.getDrawable(_mActivity, R.drawable.preview_list_shap) ?: ColorDrawable()
        )
        return decoration
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: AlbumFile?) {
        val temp = if (TextUtils.isEmpty(item?.editorPath)) item?.path else item?.editorPath
        Log.d("convertItem", "temp: $temp")
        Log.d("convertItem", "editorPath: ${item?.editorPath}")
        Log.d("convertItem", "path: ${item?.path}")
        helper?.setImageLocalUrl(R.id.preViewImgCurrent, temp)
            ?.setImageLocalUrl(R.id.preViewImg, temp)
            ?.setJsVisible(R.id.preViewImgCurrent, currentIndex == helper.layoutPosition)
            ?.setJsVisible(R.id.mPreViewCheckStatus, !(item?.isChecked ?: true))
            ?.setJsVisible(R.id.preViewImg, currentIndex != helper.layoutPosition)
    }

    override fun getLayoutManager() =
        LinearLayoutManager(_mActivity, LinearLayoutManager.HORIZONTAL, false)

    override fun getListType(): Type = TypeBuilder.newInstance(CommonBean::class.java)
        .endSubType().build()

    override fun onRefresh() {

    }

    override fun netRequest() {

    }

    fun notifyItemChanged(item: AlbumFile) {
        mCheckItem[currentIndex] = item
        mAdapter.notifyItemChanged(currentIndex)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        super.onItemClick(adapter, view, position)
        if (currentIndex == position) return

        currentIndex = if (position >= 0 && position < mCheckItem.size) position else 0

        if (mRecyclerView != null) {
            mRecyclerView.scrollToPosition(currentIndex)
        }

        val item = mAdapter.getItem(currentIndex)

        mListItemClick?.onPreviewFooterListItemClick(item)

        mAdapter.notifyDataSetChanged()
    }

    interface PreviewFooterListItemClick {
        fun onPreviewFooterListItemClick(item: AlbumFile)
    }
}